package com.mind.customer.utils;

import com.mind.common.utils.DateUtils;
import com.mind.common.utils.StringUtils;
import com.mind.customer.mapper.AjCallApiDetailMapper;
import com.mind.customer.mapper.AjCallApiMapper;
import com.mind.system.domain.AjCallApi;
import com.mind.system.domain.AjCallApiDetail;
import com.mind.system.domain.AjTicketTask;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class CountUtils {

    private Map<String, String> urlMapping = new HashMap<String, String>() {{
        put("call_ocr", "https://api.fanhanapp.com/api/v4/AI_httpService/DEV00080/main_ppocr");
        put("call_model", "https://api.fanhanapp.com/api/v4/AI_httpService/DEV00080/main_check_PI");
        put("call_stamp", "https://api.fanhanapp.com/api/v4/AI_httpService/DEV00080/main_check_stamp");
    }};

    @Resource
    private AjCallApiMapper callApiMapper;

    @Resource
    private AjCallApiDetailMapper callApiDetailMapper;

    /**
     * @param type   api类型
     * @param result api返回结果
     * @param status api调用状态
     * @param task   任务信息
     */
    public void countApi(String type, String result, Integer status, String url, AjTicketTask task, String month) {
        if(StringUtils.isEmpty(month)){
            month = DateUtils.parseDateToStr("yyyy-MM",DateUtils.getNowDate());
        }
        String finalMonth = month;
        List<AjCallApi> ajCallApis = callApiMapper.selectCallApiList(new AjCallApi() {{
            setType(type);
            setMonth(finalMonth);
        }});
        AjCallApi ajCallApi;
        if (ajCallApis.size()==0) {
            ajCallApi = new AjCallApi();
            ajCallApi.setType(type);
            ajCallApi.setUrl(urlMapping.get(type));
            ajCallApi.setStatus(1);
            ajCallApi.setMonth(finalMonth);
            if (status.equals(1)) {//成功
                ajCallApi.setCallSuccessCount(1L);
            } else {//失败
                ajCallApi.setCallErrorCount(1L);
            }
            callApiMapper.insertCallApi(ajCallApi);
        } else {
            ajCallApi = ajCallApis.get(0);
            long oldSuccessCount = ajCallApi.getCallSuccessCount() == null? 0:ajCallApi.getCallSuccessCount();
            long oldErrorCount = ajCallApi.getCallErrorCount() == null? 0:ajCallApi.getCallErrorCount();
            if (status.equals(1)) {//成功

                ajCallApi.setCallSuccessCount(oldSuccessCount + 1);
            } else {
                ajCallApi.setCallErrorCount(oldErrorCount + 1);
            }
            callApiMapper.updateCallApi(ajCallApi);
        }

        callApiDetailMapper.insertCallApiDetail(new AjCallApiDetail() {{
            setStatus(status);
            if(task != null) {
                setTicketId(task.getTicketId());
                setVersion(task.getVersion());
            }
            setApiUrl(urlMapping.get(type));
            if(StringUtils.isEmpty(url)) {
                setFileUrl("手动调用");
            }else{
                setFileUrl(url);
            }
            setCallApiId(ajCallApi.getId());
            setResult(result);
        }});
    }
}
